Clean Python by Sunil Kapil

Clean Python by Sunil Kapil

Author:Sunil Kapil
Language: eng
Format: epub, azw3
ISBN: 9781484248782
Publisher: Apress


As you know, metaclasses create classes. Just like you can create classes in order to create objects, in the same way Python metaclasses create these objects. In other words, metaclasses are classes’ classes. As this section is not about how metaclasses work, I will focus on when you should consider using metaclasses.

Most of the time you won’t need metaclasses in your code. The main use case of a metaclass is to create an API or library or add some complex feature. Whenever you want to hide a lot of detail and make it easier for the client to use your API/library, metaclasses can be really helpful to do that.

Take, for example, Django ORM, which heavily uses metaclasses to make its ORM API easy to use and understand. Django makes this possible by using metaclasses, and you write the Django ORM as shown in Listing 4-5.class User(models.Model):

name = models.CharField(max_length=30)

age = models.IntegerField()



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.